{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = tf.keras.models.Sequential([\n",
    "    # 卷积层：96 11*11 4 relu\n",
    "    tf.keras.layers.Conv2D(filters=96, kernel_size=11,\n",
    "                           strides=4, activation=\"relu\"),\n",
    "    # 池化：3*3 2\n",
    "    tf.keras.layers.MaxPool2D(pool_size=3, strides=2),\n",
    "    # 卷积：256 5*5 1 RELU same\n",
    "    tf.keras.layers.Conv2D(filters=256, kernel_size=5,\n",
    "                           padding=\"same\", activation=\"relu\"),\n",
    "    # 池化： 3*3 2\n",
    "    tf.keras.layers.MaxPool2D(pool_size=3, strides=2),\n",
    "    # 卷积：384 3*3 1 RELU same\n",
    "    tf.keras.layers.Conv2D(filters=384, kernel_size=3, padding=\"same\", activation=\"relu\"),\n",
    "    # 卷积：384 3*3 1 RELU same\n",
    "    tf.keras.layers.Conv2D(filters=384, kernel_size=3, padding=\"same\", activation=\"relu\"),\n",
    "    # 卷积：256 3*3 1 RELU same\n",
    "    tf.keras.layers.Conv2D(filters=256, kernel_size=3, padding=\"same\", activation=\"relu\"),\n",
    "    # 池化：3*3 2\n",
    "    tf.keras.layers.MaxPool2D(pool_size=3, strides=2),\n",
    "    # 展开\n",
    "    tf.keras.layers.Flatten(),\n",
    "    # 全连接层：4096 relu\n",
    "    tf.keras.layers.Dense(4096, activation=\"relu\"),\n",
    "    # 随机失活\n",
    "    tf.keras.layers.Dropout(0.5),\n",
    "    # 全连接层：4096 relu\n",
    "    tf.keras.layers.Dense(4096, activation=\"relu\"),\n",
    "    # 随机失活\n",
    "    tf.keras.layers.Dropout(0.5),\n",
    "    # 输出层：\n",
    "    tf.keras.layers.Dense(10, activation=\"softmax\")\n",
    "\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d (Conv2D)              (1, 55, 55, 96)           11712     \n",
      "_________________________________________________________________\n",
      "max_pooling2d (MaxPooling2D) (1, 27, 27, 96)           0         \n",
      "_________________________________________________________________\n",
      "conv2d_1 (Conv2D)            (1, 27, 27, 256)          614656    \n",
      "_________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2 (1, 13, 13, 256)          0         \n",
      "_________________________________________________________________\n",
      "conv2d_2 (Conv2D)            (1, 13, 13, 384)          885120    \n",
      "_________________________________________________________________\n",
      "conv2d_3 (Conv2D)            (1, 13, 13, 384)          1327488   \n",
      "_________________________________________________________________\n",
      "conv2d_4 (Conv2D)            (1, 13, 13, 256)          884992    \n",
      "_________________________________________________________________\n",
      "max_pooling2d_2 (MaxPooling2 (1, 6, 6, 256)            0         \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (1, 9216)                 0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (1, 4096)                 37752832  \n",
      "_________________________________________________________________\n",
      "dropout (Dropout)            (1, 4096)                 0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (1, 4096)                 16781312  \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (1, 4096)                 0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (1, 10)                   40970     \n",
      "=================================================================\n",
      "Total params: 58,299,082\n",
      "Trainable params: 58,299,082\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "X = tf.random.uniform((1,227,227,1))\n",
    "y = net(X)\n",
    "net.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.datasets import mnist\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_images,train_label),(test_images,test_labels)=mnist.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 维度调整\n",
    "train_images = np.reshape(train_images,(train_images.shape[0],train_images.shape[1],train_images.shape[2],1))\n",
    "test_images = np.reshape(test_images,(test_images.shape[0],test_images.shape[1],test_images.shape[2],1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对训练数据进行抽样\n",
    "def get_train(size):\n",
    "    # 随机生成index\n",
    "    index = np.random.randint(0,train_images.shape[0],size)\n",
    "    # 选择图像并进行resize\n",
    "    resized_image = tf.image.resize_with_pad(train_images[index],227,227)\n",
    "    return resized_image.numpy(),train_label[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对测试数据进行抽样\n",
    "def get_test(size):\n",
    "    # 随机生成index\n",
    "    index = np.random.randint(0,test_images.shape[0],size)\n",
    "    # 选择图像并进行resize\n",
    "    resized_image = tf.image.resize_with_pad(test_images[index],227,227)\n",
    "    return resized_image.numpy(),test_labels[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 抽样结果\n",
    "train_images,train_label = get_train(256)\n",
    "test_images,test_labels = get_test(128)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x146de3f60>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP0AAAD5CAYAAADspDPqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAX1ElEQVR4nO3db4xbV53/8ffX/+2ZjGcSUlrCgkArELSlT1K0QgltsrR5kvanXQRCP/5ULRDaSK1El31YkQeA2PCgWv4obKSVuqoqtu0iYCkPWkj7E61Qi9KWVsui1QaFH0Tq/2kmM+P/9tkH8bkcezybiWN7PD2fl3Tl6+uM546dj7/nnHvutTnnEJF4pDZ7B0RkshR6kcgo9CKRUehFIqPQi0RGoReJzEhCb2YFM3vEzF4ws/vNzEbxvCIyeqOq9J8BzjjnrgEWgBtG9LwiMmKZET3PfuCH3fXHgX3AY+v941Kp5Obn50f0q0VkkJdeeul159zO/u2jCv0OYKm7fg54f/8/MLNDwCGAcrnMl770pRH9ahEZ5MiRI/9/0PZRNe9fB8rd9XL3fg/n3HHn3G7n3O5SqTSiXysiF2tUoT8B3Nhd3w88MaLnFZERG1XoHwB2mdmLwCLnPwREZAqNpE/vnKsDB0fxXCIyXpqcIxIZhV4kMgq9SGQUepHIKPQikVHoRSKj0ItERqEXiYxCLxIZhV4kMgq9SGQUepHIKPQikVHoRSKj0ItERqEXiYxCLxIZhV4kMgq9SGQUepHIKPQikRnVN9zIFmVm+O8bDb93NJVaWw86nc6abc65nvXwvkwnhT5i6XSabDZLJpMhk8n03E+n02sC7O875+h0OrRaLVqtFu12O7ltNpu02+3N+HNkgxT6SJkZ2WyWfD5PPp+nUCgk6/l8nlwu1xPy/ttms0m9Xqder9NoNJJ1QKGfcgp9xDKZDPl8npmZmWQplUrMzMyQz+eT5noYdr9eq9WoVCrJ4rsD7XYbM1Mzf4op9JEyM9LpdBL6ubm5Z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FHqRyCj0IpFR6EUio9CLREahF4mMQi8SGYVeJDIKvUhkbDO+T8zMXgNW2drXx38bW3f/t/K+g/Z/o97tnNvZv3FTQg9gZiedc7s35ZePwFbe/62876D9v1Rq3otERqEXicxmhv74Jv7uUdjK+7+V9x20/5dk0/r0IrI51LwXiYxCLxKZiYbezApm9oiZvWBm95uZTfL3D8vMrjWzM2b2VHe5Zqv8HWaWNbOfdtfXvP7T/p707X//+/D+ad5/M/sXM3vazP7dzGan5bWfdKX/DHDGOXcNsADcMOHfP6wF4Jhzbo9zbg9wLVvg7zCzIvAsf96/Qa//1L4nA/a/531wzv0XU7r/ZrYHyDjn/gqYA25jSl77SYd+P/Dz7vrjwL4J//5hLQAfN7Nfm9kPgb9mC/wdzrmqc+5DwJnupkGv/9S+JwP2v+d96FbGad3/V4B/7K6ngCNMyWs/6dDvAJa66+eA7RP+/cM6BdzjnPswcAXwt2zNv2PQ67+V3pP+9+E6pnT/nXP/7Zz7tZn9DdABnmdKXvtJh/51oNxdL7N15k//AfhFsN5ha/4dg17/rfSe/IHe9+Eypnj/zexm4C7gJuBlpuS1n3ToTwA3dtf3A09M+PcP627gU2aWAq4C/o6t+XcMev230nvS/z78B1O6/2Z2OfD3wEHn3DJT9NpPOvQPALvM7EVgkfN/9FbwXeBW4BngR8A/szX/jkGv/1Z6T3reB+fcfzK9+38L57sgj5rZU0CWKXntNSNPJDKanCMSGYVeJDIKvUhkFHqRyCj0IpFR6EUi8z+wgPzZOOTXoQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(train_images[4].astype(np.int8).squeeze(),cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型编译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 优化器,损失函数,评价指标\n",
    "net.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),loss=tf.keras.losses.sparse_categorical_crossentropy\n",
    "           ,metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/3\n",
      "2/2 [==============================] - 3s 2s/step - loss: 197.2379 - accuracy: 0.1000 - val_loss: 15641.0518 - val_accuracy: 0.1538\n",
      "Epoch 2/3\n",
      "2/2 [==============================] - 3s 2s/step - loss: 630482274353152.0000 - accuracy: 0.1478 - val_loss: nan - val_accuracy: 0.0385\n",
      "Epoch 3/3\n",
      "2/2 [==============================] - 3s 2s/step - loss: nan - accuracy: 0.1435 - val_loss: nan - val_accuracy: 0.0385\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x1498ac278>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.fit(train_images,train_label,batch_size=128,epochs=3,validation_split=0.1,verbose=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4/4 [==============================] - 1s 168ms/step - loss: nan - accuracy: 0.0938\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[nan, 0.09375]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.evaluate(test_images,test_labels,verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
